<?php

namespace App\depository;

use App\Models\RecordBunkoModel;

class RecordBunkoDep
{
    public $model;

    public function __construct(){
        $this->model = new RecordBunkoModel();
    }

    public function add($data){
        return $this->model->insert($data);
    }


    public function get_list_by_gh_id($gh_id){
        return $this->model->where('gh_id', $gh_id)->orderBy('id', 'desc')->limit(200)->get()->toArray();
    }


    public function list($param){
        $res = $this->model
            ->when(!empty($param['id']), function ($query) use ($param) {
                return $query->where("id", $param['id']);
            })
            ->when(!empty($param['gh_id']), function ($query) use ($param) {
                return $query->where("gh_id", $param['gh_id']);
            })

            ->when(!empty($param['date'][0]),function($query) use($param){
                return $query->where("date",">=",$param['date'][0]);
            })
//            ->when(!empty($param['date'][1]),function($query) use($param){
//                return $query->where("date","<=",date("Y-m-d",strtotime("+1 day",strtotime($param['date'][1]))));
//            })
            ->when(!empty($param['date'][1]),function($query) use($param){
                return $query->where("date","<=",$param['date'][1]);
            })
            ->when(!empty($param['camp_id']), function ($query) use ($param) {
                return $query->where("camp_id",'like', "%{$param['camp_id']}%");
            })
            ->when(!empty($param['camp_name']), function ($query) use ($param) {
                return $query->where("camp_name",'like', "%{$param['camp_name']}%");
            })
            ->when(!empty($param['bunko_result']), function ($query) use ($param) {
                return $query->where("bunko_result",'like', "%{$param['bunko_result']}%");
            })
            ->when(!empty($param['ticket_num']), function ($query) use ($param) {
                return $query->where("ticket_num",'like', "%{$param['ticket_num']}%");
            })
            ->when(!empty($param['map_name']), function ($query) use ($param) {
                return $query->where("map_name",'like', "%{$param['map_name']}%");
            })
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);;
        return $res;
    }

    public function del($id){
        return $this->model->whereIn('id',$id)->delete();
    }

    public function upd($id,$data){
        return $this->model->where('id',$id)->update($data);
    }
}
